﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Water.Architecture2.IDAL
{
    [Serializable]
    public class WhereFields:IEnumerable
    {
        //private WhereField[] _whereFields;
        private List<WhereField> _whereFields = new List<WhereField>();
       
        /// <summary>
        /// 添加where条件
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="operatorType">操作符（数据库运算符）</param>
        /// <param name="value">字段值</param>
        /// <param name="ignoreCase">是否忽略大小写，默认false 不忽略大小写</param>
        public void Add(string name, OperatorType operatorType, object value, bool ignoreCase)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name", Water.Resources.Globalize.ExceptionResource.ExceptionMultilistHashtable);
            this._whereFields.Add(new WhereField(name, value, operatorType,ignoreCase));
        }
        /// <summary>
        /// 添加where条件
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="operatorType">操作符（数据库运算符）</param>
        /// <param name="value">字段值</param>
        public void Add(string name, OperatorType operatorType, object value)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name", Water.Resources.Globalize.ExceptionResource.ExceptionMultilistHashtable);
            this._whereFields.Add(new WhereField(name, value, operatorType));
        }
        /// <summary>
        /// 添加where条件
        /// </summary>
        /// <param name="whereClause">where子句</param>
        public void Add(string whereClause)
        {
            if (string.IsNullOrEmpty(whereClause))
                throw new ArgumentNullException("whereClause", Water.Resources.Globalize.ExceptionResource.ExceptionMultilistHashtable);
            this._whereFields.Add(new WhereField(whereClause));
        }

        /// <summary>
        /// 添加where条件
        /// </summary>
        /// <param name="whereField"><see cref="WhereField"/>单个where条件</param>
        public void Add(WhereField whereField)
        {
            this._whereFields.Add(whereField);
        }
        /// <summary>
        /// where字段总数
        /// </summary>
        public int Count
        {
            get { return _whereFields.Count; }
        }
        /// <summary>
        /// WhereFields索引
        /// </summary>
        /// <param name="index">索引值</param>
        /// <returns>WhereField</returns>
        public WhereField this[int index]
        {
            get { return _whereFields[index]; }

        }
        #region IEnumerable 成员

        public IEnumerator GetEnumerator()
        {
            return new WhereFieldEnumerator(this._whereFields);
        }

        #endregion

        public override string ToString()
        {

            if (this._whereFields.Count == 0)
                return base.ToString();
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (WhereField wf in _whereFields)
                {
                    sb.Append(wf.Name);
                    sb.Append("=");
                    if (wf.IsOperator)
                        sb.Append(wf.Operator.ToString()+"-"+wf.Value);
                    else
                        sb.Append(wf.WhereClause);
                }
                return sb.ToString();
            }
        } 
    }
}
